<template>
  <div ref="Panel" class="charts" style="width: 100%; height: 300px"></div>
</template>

<script lang="ts">
import * as echarts from 'echarts';

export default {
  name: 'PerformanceMonitorPanel',
  props: {
    compName: {
      type: String,
      default: 'hardware',
    },
    currentData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      timer: '',
      panelChart: '',
    };
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
  mounted() {
    let titleText = this.compName;
    const _this = this;
    if ('capacity' in this.currentData) {
      if (this.currentData.compName === 'Disk')
        titleText =
          this.compName +
          '(' +
          this.$t('pagePerformanceMonitor.totalCapacity') +
          this.currentData.capacity +
          this.currentData.capacityUnit +
          ')';
      else if (this.currentData.compName === 'Memory')
        titleText =
          this.compName +
          '(' +
          this.$t('pagePerformanceMonitor.totalCapacity') +
          this.currentData.capacity +
          this.currentData.unit +
          ')';
    }
    this.panelChart = echarts.init(this.$refs.Panel);
    this.timer = setInterval(() => {
      var usage = parseInt(this.currentData.usage);

      let title;
      if (this.currentData.compName === 'Disk')
        title = [
          {
            text: titleText,
            left: 'center',
          },
          {
            text:
              _this.$t('pagePerformanceMonitor.free') +
              ':' +
              _this.currentData.free +
              _this.currentData.freeUnit,
            top: '240',
            left: 'center',
          },
        ];
      else
        title = [
          {
            text: titleText,
            left: 'center',
          },
        ];

      const options = {
        title: title,
        tooltip: {
          formatter: '{a}: {c}%',
        },
        series: [
          {
            name: _this.$t('pagePerformanceMonitor.usage'),
            type: 'gauge',
            detail: {
              formatter: '{value}%',
              textStyle: {
                fontSize: 18,
              },
            },
            data: [{ value: usage }],
          },
        ],
      };

      this.panelChart.setOption(options, {
        notMerge: false,
        lazyUpdate: true,
        silent: false,
      });
      this.panelChart.resize();
    }, 1000);
  },
};
</script>
